#============================= Filebeat modules ===============================
filebeat.config.modules:
  # Glob pattern for configuration loading
  path: /etc/filebeat/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: true

  # Period on which files under path should be checked for changes
  reload.period: 30s

#------------------------------ Log inputs --------------------------------
filebeat.inputs:
{{ filebeat_extra_input | default('') }}
{# ------------------- nodes ------------------- #}
- type: log
  enabled: true
  paths:
    - /var/log/monit.log
  multiline.pattern: '^\[CST'
  multiline.negate: true
  multiline.match: after
  tags: ["monit"]

- type: log
  enabled: true
  paths:
    - /var/log/filebeat/filebeat
    - /var/log/metricbeat/metricbeat
    - /var/log/packetbeat/packetbeat
  exclude_lines: [
    "connection reset by peer",
    "Failed to connect"
  ]
  tags: ["beats-log"]

#============================== Output =====================================

{% if (groups['redis'] | count) >= 3 %}
output.redis:
  hosts: ["redis.service.{{ consul_dc_name }}.consul:6379"]
  key: "filebeat"
  db: 0
  timeout: 5
{% elif (groups['logstash'] | count) >= 1 %}
output.logstash:
  hosts: ["logstash.service.{{ consul_dc_name }}.consul:{{ logstash_beats_port }}"]
  compression_level: 5
  timeout: 300
  slow_start: true
{% elif (groups['elasticsearch'] | count) >= 1 %}
output.elasticsearch:
  hosts: ["ingest.elastic.service.{{ consul_dc_name }}.consul:{{ elastic_http_port }}"]
  compression_level: 5
  timeout: 300
  slow_start: true
{% endif %}

{% if inventory_hostname == groups['all'][0] %}
#============================== Dashboards =====================================
setup.dashboards:
  enabled: true
  elasticsearch:
    {% if searchguard_enabled -%}
    hosts: ["https://ingest.elastic.service.{{ consul_dc_name }}.consul:{{ elastic_http_port }}"]
    username: kibanaserver
    password: {{ sg_users | selectattr('id', 'eq', 'kibanaserver') | map(attribute='pass') | first }}
    protocal: https
    ssl:
      verification_mode: none
    {% else -%}
    hosts: ["http://ingest.elastic.service.{{ consul_dc_name }}.consul:{{ elastic_http_port }}"]
    {% endif -%}
  retry:
    enabled: true
    interval: 60s
    maximum: 10

#================================ Kibana =====================================
setup.kibana:
  host: "kibana.service.{{ consul_dc_name }}.consul"

{% endif %}
#================================ Logging =====================================

logging:
  to_files: true
  level: error
  files:
    rotateeverybytes: 10485760 # = 10MB
    keepfiles: 7

#================================ Processors =====================================

# Configure processors to enhance or manipulate events generated by the beat.

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~

#================================ General =====================================

fields_under_root: true
fields:
    beat_type: filebeat

name: {{ inventory_hostname }}

tags: [
{%- for item in group_names -%}
  "{{ item }}"{%- if loop.nextitem is defined -%},{%- endif -%}
{%- endfor -%}
]

{% if '6.3.0' is version(elk_version, operator='lt', strict=True) %}
#============================== Xpack Monitoring ===============================
xpack.monitoring:
  enabled: true
  elasticsearch:
    {% if searchguard_enabled -%}
    hosts: ["https://ingest.elastic.service.{{ consul_dc_name }}.consul:{{ elastic_http_port }}"]
    username: haibin
    password: {{ sg_users | selectattr('id', 'eq', 'haibin') | map(attribute='pass') | first }}
    ssl:
      verification_mode: none
    {% else -%}
    hosts: ["http://ingest.elastic.service.{{ consul_dc_name }}.consul:{{ elastic_http_port }}"]
    {% endif -%}
{% endif %}
